const conn = require("./conn.js");
var _SQL={
// 查询列表数据并分页 
    findlist(req, res,tableName) {
    return new Promise((resolve, reject) => {
        var pageNum=req.query.pageNum ? Number(req.query.pageNum)  : 1
        var pageSize= req.query.pageSize  ? Number(req.query.pageSize) : 10
        var reqdata={
            star:(pageNum- 1 ) * pageSize ,
            end :  pageSize * pageNum,
        }
        let sql = `select * from ${tableName} ORDER BY id DESC limit ? , ?`;
        let data=[reqdata.star,reqdata.end]
        let lensql=`SELECT COUNT(*) as count FROM ${tableName}`
        conn.query(lensql,data,(errs,results)=>{
            if (errs) {
                return res.send({ code:0,msg:errs,data:'err'})
            }
            conn.query(sql,data,(err,result)=>{
                if (err) {
                    return res.send({ code:0,msg:err,data:'err'})
                }
                res.send({ code:200,msg:'OK',data:{
                    list:result,
                    count:results[0].count,
                    ...reqdata
                }})
            })
             
        })
    })
    },
    insertData(req, res,tableName){
        let insertSql = `insert into ${tableName} set ?`;
        let datas =req.body
        conn.query(insertSql, datas,function (error, results) {
            if (error) {
                res.send({ code:0,msg:error,data:11})
                return false
            }else{
                res.send({ code:200,msg:'OK',data:22})
            }
        });
    },
    getDataInfoById(req, res,tableName){
        let sql =   `select * from ${tableName} where id = ?`;
        let data = [req.query.id]
        conn.query(sql,data,(err,result)=>{
            if (err) {
                return res.send({ code:0,msg:err,data:''})
            }
            res.send({ code:200,msg:'OK',data:result[0]})
        })
    },
    
    deleteDataById(req, res,tableName){
        let deleteSql =`delete from ${tableName} where id = ?`;
        let data = [req.body.id]
        conn.query(deleteSql, data,function (error, results) {
            if (error) {
                res.send({ code:0,msg:error,data:null})
                return false
            }else{
                // affectedRows是受影响的行数
                if (results.affectedRows==0) {
                    res.send({ code:0,msg:results,data:null})
                    return false
                }
                res.send({ code:200,msg:'OK',data:null})
            }
        });
    },
    updateById(req, res,tableName){
        let updateSql =`update ${tableName} set ? where id = ?`;
        let data = [req.body,req.body.id]
        conn.query(updateSql, data,function (error, results) {
            if (error) {
                res.send({ code:0,msg:error,data:null})
                return false
            }else{
                // changedRows改变行数为0说明没有内容更新
                if (results.affectedRows==0) {
                    res.send({ code:0,msg:results,data:results})
                    return false
                }
                res.send({ code:200,msg:'修改成功',data:results})
            }
        });
    }
}
module.exports = _SQL;